<template>
  <div class="my-container">
    <div
      class="header"
      v-if="!user"
    >
      <img
        src="~@/assets/mobile.png"
        class="mobile-img"
        @click="$router.push('/login')"
      >
      <span class="text">登录 / 注册</span>
    </div>
    <div
      class="header-info"
      v-else
    >
      <div class="top">
        <div class="left">
          <van-image
            round
            fit="cover"
            class="avatar"
            :src="userInfo.photo"
          />
          <span class="text">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button
            type="default"
            size="mini"
            round
            to="/user/profile"
          >编辑资料</van-button>
        </div>
      </div>
      <div class="bottom">
        <div class="data-item">
          <span class="text-number">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="text-number">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="text-number">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="text-number">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <van-grid
      column-num="2"
      class="nav-grid"
      clickable
    >
      <van-grid-item>
        <i
          slot="icon"
          class="toutiao toutiao-shoucang"
        ></i>
        <span
          slot="text"
          class="text"
        >收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i
          slot="icon"
          class="toutiao toutiao-lishi"
        ></i>
        <span
          slot="text"
          class="text"
        >历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell
      title="消息通知"
      is-link
    />
    <van-cell
      title="小智同学"
      is-link
    />
    <van-cell
      class="logout-cell"
      title="退出登录"
      center
      v-if="user"
      @click="outFn"
      clickable
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'MyIndex',
  components: {},
  props: {},
  data () {
    return {
      userInfo: {} // 用户信息
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {
    user () {
      this.userInfo = {}
      if (this.user) {
        this.loadUser()
      }
    }
  },
  created () {
    if (this.user) {
      this.loadUser()
    }
  },
  mounted () { },
  methods: {
    outFn () {
      this.$dialog.confirm({
        title: '确认退出？'
      })
        .then(() => {
          // on confirm
          this.$store.commit('setUser', null)
          // this.$router.go(0)
          // console.log(666)
          document.location.reload()
        })
        .catch(() => {
          // on cancel
        })
    },
    async loadUser () {
      try {
        const { data } = await getUserInfo()
        // console.log(data)
        this.userInfo = data.data
      } catch (err) {
        console.log(err)
        this.$toast('获取用户信息失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  .header {
    height: 361px;
    background: url("~@/assets/banner.png") no-repeat;
    background-size: cover;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .mobile-img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
    }
    .text {
      font-size: 30px;
      color: #fff;
    }
  }
  .header-info {
    height: 361px;
    background: url("~@/assets/banner.png") no-repeat;
    background-size: cover;
    .top {
      height: 231px;
      padding: 75px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        .avatar {
          width: 130px;
          height: 130px;
          vertical-align: middle;
          margin-right: 22px;
          border: 2px solid #fff;
        }
        .text {
          color: #fff;
          font-size: 30px;
        }
      }
    }
    .bottom {
      height: 130px;
      display: flex;
      align-items: center;
      .data-item {
        flex: 1;
        text-align: center;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: #fff;
        .text {
          font-size: 23px;
        }
        .text-number {
          font-size: 36px;
          margin-bottom: 15px;
        }
      }
    }
  }
  .nav-grid {
    background: #fff;
    margin-bottom: 9px;
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    .text {
      color: #000;
      margin-top: 8px;
      font-size: 28px;
    }
  }
  .logout-cell {
    height: 100px;
    text-align: center;
    color: #eb5253;
    margin: 10px 0;
    font-size: 30px;
  }
}
</style>
